package org.session.model;

import java.util.Collection;
import java.util.Date;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Session object serves as the unit of work for SessionService operations.
 */
public final class SessionImpl implements Session, RestrictedSessionContract {
    private static final long serialVersionUID = 1L;
    private final String sessionId;
    private final Date creationDate;
    private Date lastUpdateDate;
    private AtomicBoolean isActive; // coarse states of active or passive
    private long sizeBytes; // TODO: implement me
    private RepositoryType primaryRepository;
    private RepositoryType secondaryRepository;
    // TODO: find a better datastruct for entries storage
    private Vector<SessionEntry> entries = new Vector<SessionEntry>();

    public SessionImpl(RepositoryType primaryRepository,
            RepositoryType secondaryRepository) {
        // TODO: vm-agnostic unique, something like Snowflake
        sessionId = Long.toString(System.nanoTime());
        isActive = new AtomicBoolean(Boolean.TRUE);
        creationDate = new Date();
        lastUpdateDate = new Date();
        if (primaryRepository != null) {
            this.primaryRepository = primaryRepository;
        } else {
            throw new IllegalArgumentException(
                    "Primary repository cannot be null");
        }
        this.secondaryRepository = secondaryRepository;
    }

    @Override
    public AtomicBoolean getIsActive() {
        return isActive;
    }

    @Override
    public String getSessionId() {
        return sessionId;
    }

    @Override
    public synchronized long getSizeBytes() {
        return sizeBytes;
    }

    @Override
    public synchronized Date getLastUpdateDate() {
        return lastUpdateDate;
    }

    /**
     * TODO: set-up lazy-load semantics
     */
    @Override
    public synchronized Collection<SessionEntry> getEntries() {
        return entries;
    }

    /*
     * Add given SessionEntry to already-existing entries in the Session. This
     * works like an append instead of a replace operation.
     */
    @Override
    public synchronized void addEntry(SessionEntry entry) {
        entries.add(entry);
    }

    @Override
    public synchronized void deleteEntry(SessionEntry entry) {
        entries.remove(entry);
    }

    @Override
    public synchronized void replaceEntry(SessionEntry entry) {
        deleteEntry(entry);
        addEntry(entry);
    }

    @Override
    public synchronized void setEntries(Vector<SessionEntry> entries) {
        this.entries = entries;
    }

    @Override
    public Date getCreationDate() {
        return creationDate;
    }

    @Override
    public RepositoryType getPrimaryRepositoryType() {
        return primaryRepository;
    }

    @Override
    public RepositoryType getSecondaryRepositoryType() {
        return secondaryRepository;
    }

    // User-modifications through these operations will be ignored on the
    // server-side
    @Override
    public void touchIsActive(AtomicBoolean isActive) {
        this.isActive = isActive;
    }

    @Override
    public void setSizeBytes() {
        // TODO: implement me
    }

    @Override
    public synchronized void touchLastUpdateDate() {
        this.lastUpdateDate.setTime(System.nanoTime()); // TODO: improve this
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((creationDate == null) ? 0 : creationDate.hashCode());
        result = prime * result + ((entries == null) ? 0 : entries.hashCode());
        result = prime * result
                + ((isActive == null) ? 0 : isActive.hashCode());
        result = prime * result
                + ((lastUpdateDate == null) ? 0 : lastUpdateDate.hashCode());
        result = prime
                * result
                + ((primaryRepository == null) ? 0 : primaryRepository
                        .hashCode());
        result = prime
                * result
                + ((secondaryRepository == null) ? 0 : secondaryRepository
                        .hashCode());
        result = prime * result
                + ((sessionId == null) ? 0 : sessionId.hashCode());
        result = prime * result + (int) (sizeBytes ^ (sizeBytes >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SessionImpl other = (SessionImpl) obj;
        if (creationDate == null) {
            if (other.creationDate != null)
                return false;
        } else if (!creationDate.equals(other.creationDate))
            return false;
        if (entries == null) {
            if (other.entries != null)
                return false;
        } else if (!entries.equals(other.entries))
            return false;
        if (isActive == null) {
            if (other.isActive != null)
                return false;
        } else if (!isActive.get() == other.isActive.get())
            return false;
        if (lastUpdateDate == null) {
            if (other.lastUpdateDate != null)
                return false;
        } else if (!lastUpdateDate.equals(other.lastUpdateDate))
            return false;
        if (primaryRepository != other.primaryRepository)
            return false;
        if (secondaryRepository != other.secondaryRepository)
            return false;
        if (sessionId == null) {
            if (other.sessionId != null)
                return false;
        } else if (!sessionId.equals(other.sessionId))
            return false;
        if (sizeBytes != other.sizeBytes)
            return false;
        return true;
    }

}
